package com.binguo.wxapi.transfer;

import com.binguo.common.enums.CardType;
import com.binguo.model.model.Banner;
import com.binguo.model.model.PayCard;
import com.binguo.model.model.Shop;
import com.binguo.model.repository.PayCardRepository;
import com.binguo.model.wxapi.vo.*;
import com.binguo.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by haibang on 18/5/20.
 */
@Component
public class WXCardsTransfer {

    @Autowired
    ShopService shopService;

    @Autowired
    PayCardRepository payCardRepository;


    public List<BannerVO> toBannerVOList(List<Banner> banners) {

        if(banners.isEmpty()){
            return new ArrayList<>();
        } else {
            return banners.stream().map(banner ->
                    BannerVO.builder()
                            .pic(banner.getPic())
                            .url(banner.getUrl()).build()
            ).collect(Collectors.toList());
        }
    }

    public List<CardsVO> toShopsVOList(List<PayCard> cards){

        if(cards.isEmpty()) {
            return new ArrayList<>();
        } else {
            List<CardsVO> list = new ArrayList<>();

            Map<Byte, List<PayCard>> cardTypeMap = cards.stream().collect(Collectors.groupingBy(PayCard::getCardtype));

            cardTypeMap.forEach((k,v) -> {
                CardsVO vo = CardsVO.builder()
                        .cardType(k.intValue())
                        .payCards(v.stream().map(card ->{
                            Shop shop = shopService.findShopById(card.getShopid());
                            String cardImgUrl = card.getCardtype() == CardType.BINGUO_CARD.getCode().byteValue()
                                    ? shop.getCardimgurl() : shop.getEntitycardimg();
                            /*if (shop.getPayType() == 8 && card.getCardtype() == CardType.YKT_CARD.getCode().byteValue()){
                                JinQiaoPayCardItem jinQiaoPayCardItem = JinQiaoPayController.GetCardInfoByMemInterface(card.getCardno());
                                Long balance = jinQiaoPayCardItem.getCurrentMoney();
                                card.setAccountbalance(balance);
                                payCardRepository.update(card);
                            }*/
                               return  CardVO.builder()
                                            .id(card.getId())
                                            .customerId(card.getCustomerid())
                                            .shopId(card.getShopid())
                                            .cardNo(card.getCardno())
                                            .name(card.getName())
                                            .accountBalance(card.getAccountbalance() + card.getWxAccountbalance() + card.getYktAccountbalance()+card.getBzAccountbalance())
                                            .cardType(card.getCardtype().intValue())
                                            .cardImgUrl(cardImgUrl)
                                            .isMaster(card.getIsmaster())
                                            .isNoPasswordPay(card.getIsnopasswordpay()).build();

                                }
                        ).collect(Collectors.toList())).build();
                list.add(vo);
            });
            return list;
        }


    }



}
